package com.wjyf.reducing_bento.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.exception.BusinessException;
import com.wjyf.reducing_bento.mapper.UserMapper;
import com.wjyf.reducing_bento.model.entity.InviteRecord;
import com.wjyf.reducing_bento.model.entity.InviteReward;
import com.wjyf.reducing_bento.model.entity.UserBean;
import com.wjyf.reducing_bento.model.vo.InviteRecordVo;
import com.wjyf.reducing_bento.service.InviteRecordService;
import com.wjyf.reducing_bento.mapper.InviteRecordMapper;
import com.wjyf.reducing_bento.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.wjyf.reducing_bento.constant.InviteRecordConstant.INVITATION_SUCCESS_REMARKS;

/**
 * @author 26439
 * @description 针对表【tb_invite_record(邀请记录表)】的数据库操作Service实现
 * @createDate 2023-04-29 19:14:29
 */
@Service
public class InviteRecordServiceImpl extends ServiceImpl<InviteRecordMapper, InviteRecord>
        implements InviteRecordService {

    @Resource
    private InviteRecordMapper inviteRecordMapper;

    @Resource
    private UserService userService;


    @Override
    public void bindInviter(String invitationCode, Long uId) {
        UserBean userBean = userService.getOne(new LambdaQueryWrapper<UserBean>().eq(UserBean::getInviteCode, invitationCode));
        if (userBean == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "邀请码不存在");
        }
        InviteRecord inviteReward = new InviteRecord()
                .setInviterId(userBean.getUId())
                .setInviteeId(uId)
                .setRemark(INVITATION_SUCCESS_REMARKS);
        boolean save = this.save(inviteReward);
        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "新用户绑定邀请用户失败");
        }
    }

    @Override
    public Page<InviteRecordVo> userGetInviteRecordList(Long current, Long pageSize, Integer flag, HttpServletRequest request) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserBean curUser = userService.getUser(request);

        Long total = inviteRecordMapper.selectPageToInviteRecordVoListCount(flag, curUser.getUId());

        List<InviteRecordVo> inviteRecordVoList = inviteRecordMapper.selectPageToInviteRecordVoList(
                current > 0 ? (current - 1) * pageSize : 0,
                pageSize * (current > 0 ? 1 : 0),
                flag, curUser.getUId());
        Page<InviteRecordVo> recordVoPage = new Page<>(current, pageSize);
        recordVoPage.setRecords(inviteRecordVoList)
                .setTotal(total);
        return recordVoPage;
    }

    @Override
    public Page<InviteRecordVo> adminGetInviteRecordList(Long current, Long pageSize, Integer flag) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Long total = inviteRecordMapper.selectPageToInviteRecordVoListCount(flag, null);

        List<InviteRecordVo> inviteRecordVoList = inviteRecordMapper.selectPageToInviteRecordVoList(
                current > 0 ? (current - 1) * pageSize : 0,
                pageSize * (current > 0 ? 1 : 0),
                flag, null);
        Page<InviteRecordVo> recordVoPage = new Page<>(current, pageSize);
        recordVoPage.setRecords(inviteRecordVoList)
                .setTotal(total);
        return recordVoPage;
    }
}




